package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.Food;
import com.itheima.POJO.HealthScheme;
import com.itheima.POJO.Sport;
import com.itheima.POJO.Template;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.TemplateService;
import org.apache.ibatis.annotations.Param;
import org.springframework.mail.MailParseException;
import org.springframework.web.bind.annotation.*;

import java.awt.geom.FlatteningPathIterator;
import java.util.List;
import java.util.Map;

/**
 * @auther zhaorui
 * @date 2019-12-05 - 15:43
 */
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Reference
    private TemplateService templateService;


    /**
     * 添加功能
     * @return
     */

    @RequestMapping("/add")
    public Result add(@RequestBody Template template,Integer[] healthIds,Integer[] foodIds , Integer[] sportIds){
        try {
            //调用方法 add方法添加模板
            templateService.add(template,healthIds,foodIds,sportIds);
            return new Result(true,MessageConstant.ADD_TEMPLATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TEMPLATE_FAIL);

        }
    }


    /**
     * 分页功能
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

            PageResult pageResult = templateService.findPage(queryPageBean);
            return pageResult;

    }


    /**
     * 禁用/启用功能
     *      传递多个参数的时候需要加上@Param进行区分
     *      且在SQL语句中的条件顺序 一定和 传递参数的顺序一一对应
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/forbidden")
    public Result forbiddenById(@Param("id") Integer id,@Param("status") String status){
        try {
            templateService.forbiddenById(id,status);
            return new Result(true,MessageConstant.FORBIDDEN_TEMPLATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.FORBIDDEN_TEMPLATE_FAIL);
        }
    }


    /**
     *  -- 暂时任务
     * 查询所有 健康方案
     * @return
     */
    @RequestMapping("/findAllHealth")
    public Result findAllScheme(){
        try {
            List<HealthScheme> schemeList =  templateService.findAllHealth();
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,schemeList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);
        }
    }

    /**
     *  -- 暂时任务
     * 查询所有 健康方案
     * @return
     */
    @RequestMapping("/findAllFood")
    public Result findAllFood(){
        try {
            List<Food> foodList =  templateService.findAllFood();
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,foodList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);
        }
    }


    /**
     *  -- 暂时任务
     * 查询所有 健康方案
     * @return
     */
    @RequestMapping("/findAllSport")
    public Result findAllSport(){
        try {
            List<Sport> sportList =  templateService.findAllSport();
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,sportList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);
        }
    }

    /**
     * 根据id 查询template对象
     * @param id
     * @return
     */
    @RequestMapping("/findTemplateById")
    public Result findTemplateById(Integer id){
        try {
            Template template = templateService.findTemplateById(id);
            return new Result(true,MessageConstant.QUERY_TEMPLATE_SUCCESS,template);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_TEMPLATE_FAIL);
        }
    }


    /**
     * 根据templateID 查询相关联中间表的healthschemeIDS
     * @param id
     * @return
     */
    @RequestMapping("/findTemplateAndHealthByTemplateId")
    public Result findTemplateAndHealthByTemplateId(Integer id){
        try {
            List<Integer> healthIds = templateService.findTemplateAndHealthByTemplateId(id);
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,healthIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);

        }
    }


    @RequestMapping("/findTemplateAndFoodByTemplateId")
    public Result findTemplateAndFoodByTemplateId(Integer id){
        try {
            List<Integer> foodIds = templateService.findTemplateAndFoodByTemplateId(id);
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,foodIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);

        }
    }


    @RequestMapping("/findTemplateAndSportByTemplateId")
    public Result findTemplateAndSportByTemplateId(Integer id){
        try {
            List<Integer> sportIds = templateService.findTemplateAndSportByTemplateId(id);
            return new Result(true,MessageConstant.QUERY_SCHEME_SUCCESS,sportIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SCHEME_FAIL);

        }
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Template template ,Integer[] healthIds,Integer[] foodIds , Integer[] sportIds ){
        try {
            templateService.edit(template,healthIds,foodIds,sportIds);
            return new Result(true,MessageConstant.EDIT_TEMPLATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TEMPLATE_FAIL);
        }
    }


    @RequestMapping(value = "/delete")
    public Result delete(Integer id){
        try {
            templateService.delete(id);
            return new Result(true,MessageConstant.FORBIDDEN_TEMPLATE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());

        }catch (Exception e){
            return new Result(false ,MessageConstant.FORBIDDEN_TEMPLATE_FAIL);
        }
    }
}
